import { getToken, refund, refundDetail } from "../../../api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pic_lis: [],
    showImages: [],
    count: 9,
    token_pic: '',
    sale_name: '',
    sale_pic: '',
    sale_money: '',
    sale_barchcode: '',
    slaemoney_val: '',
    refundDetail_state: false,
    refundDetail_list: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.setData({
      sale_name: options.name,
      sale_pic: options.pic,
      sale_money: options.money,
      sale_barchcode: options.batchcode,
    })
    refundDetail(
      getApp().globalData.Store.token,
      options.batchcode,
    ).then(res => {
      if (res.data.code == 666) {
        that.setData({
          refundDetail_state: true,
          refundDetail_list: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: that.data.count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.concat(tempFilePaths)
        that.setData({
          count: that.data.count - tempFilePaths.length,
          showImages: showImages,
        })
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, 20)
        })
      }
    })
  },
  uplod: function (obj, num) {
    var that = this
    var showImages = that.data.showImages
    for (var i = 0; i < obj.length; i++) {
      var timestamp = Date.parse(new Date()) + [i]
      var type = obj[i].substring(obj[i].lastIndexOf("."), obj[i].length);
      var keyy = timestamp + type
      wx.uploadFile({
        url: 'https://upload.qiniup.com',
        filePath: obj[i],
        name: 'file', //文件的key 
        header: {
          "Content-Type": "multipart/form-data"
        },
        formData: {
          token: that.data.token_pic,
          key: keyy
        }, //传递参数
        success: function (res) {
          var data = JSON.parse(res.data)
          var singleImg = {};
          if (num != 20) {
            // that.data.addEdit.image[num].pic_url = data.key
            // that.data.addEdit.image[num].id = that.data.showImages[num].id
          } else {
            var image = that.data.pic_lis;
            var min;
            singleImg = data.key
            image.push(singleImg)
            for (var i = 0; i < image.length; i++) {
              for (var j = i; j < image.length; j++) {
                if (parseInt(image[i]) > parseInt(image[j])) {
                  min = image[j];
                  image[j] = image[i];
                  image[i] = min;
                }
              }
            }
          }
          that.setData({
            pic_lis: that.data.pic_lis
          })
        }
      })
    }
  },
  //替换img
  changeImg: function (e) {
    var inde = e.currentTarget.dataset.key
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.splice(inde, 1, tempFilePaths[0])
        that.setData({
          showImages: that.data.showImages
        })
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, inde)
        })
      }
    })
  },
  //图片删除
  img_delete(e) {
    var index = e.currentTarget.dataset.index
    var node = this.data.showImages
    this.data.pic_lis.splice(index, 1)
    node.splice(index, 1)
    var that = this
    var that_count = 0
    that_count = 9 - that.data.showImages.length
    this.setData({
      showImages: this.data.showImages,
      count: that_count
    })
  },
  formSubmit: function (e) {
    var that = this
    refund(
      getApp().globalData.Store.token,
      that.data.sale_barchcode,
      e.detail.value.saleMoney,
      that.data.pic_lis,
      e.detail.value.saleText
    ).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: '申请成功',
          icon: 'none'
        })
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];  //上一个页面
        var list = prevPage.data.order.order_info
        for (var i = 0; i < list.length; i++) {
          if (list[i].info_batchcode == that.data.sale_barchcode) {
            list[i].order_refund_status = 1
            prevPage.setData({
              order: prevPage.data.order
            })
          }
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  saleMoney(e) {
    var num = e.detail.value
    var that = this
    if (Number(num) > Number(that.data.sale_money)) {
      wx.showToast({
        title: '请填写正确金额',
        icon: 'none'
      })
      that.setData({
        slaemoney_val: ""
      })
      return
    }
  },
  fanhui() {
    wx.navigateBack({
      delta: 1
    })
  }
})